<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\StoreCart;
use App\Models\StoreGoods;
use App\Service\CommonService;
use Illuminate\Http\Request;

class CartController extends Controller
{
    /**
     * User: YKY
     * DateTime: 2022/11/22 13:45
     * @return array
     * 购物车列表
     */
    public function cartList(){
        $uid=\request()->input('uid');
        try {
            $cartData=StoreCart::with('getGoods')->where('uid',$uid)->get();
            return CommonService::success(200,'查询成功',$cartData);
        }catch (\Exception $e){
            return CommonService::success(9999,$e->getMessage());
        }
    }

    /**
     * User: YKY
     * DateTime: 2022/11/22 20:51
     * @return array
     * 加入购物车
     */
    public function addCart(){
        $uid=\request()->input('uid');
        $gid=\request()->input('id');
        try {
            $cartData=StoreGoods::where('id',$gid)->first();
            $addCart=[
                'uid'=>$uid,
                'product_id'=>$cartData['id'],
                'cart_num'=>'1',
                'add_time'=>strtotime(date('Y-m-d h:i:s'))
            ];
            StoreCart::create($addCart);
            return CommonService::success(200,'加入购物车成功','');
        }catch (\Exception $e){
            return CommonService::success(9999,$e->getMessage());
        }
    }
}
